﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using GameProtocol;
using UnityMultiPlayerGameServer.Clients;

namespace UnityMultiPlayerGameServer.Manager
{
    /// <summary>
    /// 为各controller分发任务
    /// </summary>
    internal class ControllerManager
    {
        private Dictionary<MessageType,BaseController> controllerDict = new Dictionary<MessageType,BaseController>();

        private static readonly object locker = new object();
        private static ControllerManager instance;
        public static ControllerManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new ControllerManager();
                        }
                    }
                }
                return instance;
            }
        }

        private ControllerManager()
        {
            controllerDict.Add(MessageType.User, new UserController());
            controllerDict.Add(MessageType.Room, new RoomController());
            controllerDict.Add(MessageType.GameSync, new GameSyncController());
        }
        /// <summary>
        /// 处理由byte数组得到的GamePack对象
        /// </summary>
        /// <param name="pack"></param>
        public void PackHandel(GamePack pack, Client client)
        {
            if (pack == null) return;
            if(controllerDict.TryGetValue(pack.MessageType, out BaseController controller))
            {
                //获取与行为代码对应的方法名
                string methodName = pack.ActionCode.ToString();
                //通过反射获取与方法名对应的方法
                MethodInfo method = controller.GetType().GetMethod(methodName);

                if(method == null)
                {
                    Console.WriteLine(client.Socket.RemoteEndPoint.Serialize().ToString()
                                        + ": 未找到" + methodName + "方法名对应的方法");
                    return;
                }

                object ret = method.Invoke(controller, new object[] { client, pack });
                if(ret != null)
                {
                    client.Send(ret as GamePack);
                }
            }
        }

        public void PackHandel_UDP(GamePack pack, Client client)
        {
            if (pack == null) return;
            if (controllerDict.TryGetValue(pack.MessageType, out BaseController controller))
            {
                
                //获取与行为代码对应的方法名
                string methodName = pack.ActionCode.ToString();
                //通过反射获取与方法名对应的方法
                MethodInfo method = controller.GetType().GetMethod(methodName);

                if (method == null)
                {
                    Console.WriteLine(client.Socket.RemoteEndPoint.Serialize().ToString()
                                        + ": 未找到" + methodName + "方法名对应的方法");
                    return;
                }

                method.Invoke(controller, new object[] { client, pack });
            }
        }

    }
}
